/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
    long prev = LONG_MIN; // 存储中序遍历前驱的值
public:
    bool isValidBST(TreeNode* root) {
        if(root == nullptr)
            return true;
 
        bool left = isValidBST(root->left); // 判断左子树
        if(left == false) // 剪枝
            return false;
 
        bool flag = false;
        if(root->val > prev) // 判断自己
        {
            flag = true;
        }
        if(flag == false) // 剪枝
            return false;
            
        prev = root->val;
        bool right = isValidBST(root->right); // 判断右子树
        return left && right && flag;
    }
};